package pingan.com.imandtwo.utils;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.hyphenate.EMContactListener;
import com.hyphenate.chat.EMClient;

import java.util.List;

import chendemin.bwei.com.myapplication.gen.DbBeanDao;
import pingan.com.imandtwo.R;
import pingan.com.imandtwo.db.DbBean;
import pingan.com.imandtwo.db.DbUtils;


public class BaseActivity extends FragmentActivity {

    private DbBeanDao dbDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);

        //隐藏状态栏
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        //创建表
        dbDao = DbUtils.getSingleton().getDbDao(this);

        //全局监听器
        EMClient.getInstance().contactManager().setContactListener(new EMContactListener() {
            @Override
            public void onContactAdded(String s) {

            }

            @Override
            public void onContactDeleted(String s) {

            }

            @Override
            public void onContactInvited(String username, String message) {
                List<DbBean> dbBeen = dbDao.queryRaw("where name=?", username);
                Log.e("TAG","查到的:"+dbBeen.get(0).getName());
                if(TextUtils.isEmpty(dbBeen.get(0).getName())){
                    Log.e("TAG","走了没有");
                    DbBean dbBean = new DbBean();
                    dbBean.setName(username);
                    dbBean.setMessage(message);
                    dbBean.setType(false);
                    long insert = dbDao.insert(dbBean);
                    Log.i("CDM", "插入了: " + insert);
                }else{
                    Log.e("TAG","走了有的");
                }
            }

            @Override
            public void onFriendRequestAccepted(String s) {

            }

            @Override
            public void onFriendRequestDeclined(String s) {

            }
        });
    }
}
